package com.hollycrm.unicom.logsupport.hibernate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.logsupport.LogManager;
import com.hollycrm.unicom.logsupport.LogObject;
import com.hollycrm.unicom.logsupport.LogonLog;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class HibernateLogonLogManager extends HibernateDaoSupport implements LogManager {
    /**
     * writeLog
     *
     * @param logObject LogObject
     * @return LogObject
     */
    public LogObject writeLog(LogObject logObject) {
        HibernateTemplate ht = getHibernateTemplate();
        LogonLog logonLog = (LogonLog) logObject;
        logonLog.checkNull();
        ht.save(logonLog);
        return logonLog;
    }

    /**
     * updateLog
     *
     * @param logObject LogObject
     * @return LogObject
     */
    public LogObject updateLog(final LogObject logObject) {
        HibernateTemplate ht = getHibernateTemplate();
        LogonLog logonLog = (LogonLog) logObject;
        logonLog.checkNull();
        return (LogObject) ht.execute(new UpdateLog_Callback(logonLog));
    }

    /**
     * UpdateLog_Callback
     */
    class UpdateLog_Callback implements HibernateCallback {

        private LogonLog logonLog;

        /**
         *
         * @param logonLog LogonLog
         */
        public UpdateLog_Callback(LogonLog logonLog) {
            this.logonLog = logonLog;
        }

        /**
         * doInHibernate
         *
         * @param session Session
         * @return Object
         */
        public Object doInHibernate(Session session) {
            try {
                LogonLog entity = (LogonLog) session.find("from log in " +
                    LogonLog.class +" where log.logonID = ?",
                    logonLog.getLogonID(), Hibernate.STRING).get(0);

                SimpleDateFormat sdf = (SimpleDateFormat) DefaultBeanFactory.
                    getBean("dateFormatYear2second");

                long logonTimeMills = 0;

                try {
                    logonTimeMills = sdf.parse(entity.getLogonTime()).getTime();
                }
                catch (ParseException ex) {
                    logger.warn("" + ex.getMessage(), ex);
                }

                long logonLength = System.currentTimeMillis() - logonTimeMills;
                entity.setTimeLength( (int) (logonLength / 1000));
                entity.setLogoutTime(sdf.format(new Date(System.
                    currentTimeMillis())));
                entity.setStatus(LogonLog.STATUS_LOGOUT);

                session.saveOrUpdate(entity);
                return entity;
            }
            catch (Exception e) {
                logger.warn("[w] " + e.getMessage());
                return null;
            }
        }

    }

}
